package com.lam.system.service.impl;

import java.util.ArrayList;
import java.util.List;

import org.apache.ibatis.logging.LogFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.enums.SqlMethod;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.lam.common.utils.NumberUtil;
import com.lam.common.utils.StringUtils;
import com.lam.system.entity.SysRoleMenu;
import com.lam.system.mapper.SysRoleMenuMapper;
import com.lam.system.param.SysRoleMenuParam;
import com.lam.system.service.SysRoleMenuService;

/**
 * 角色菜单Service业务层处理
 * 
 * @author admin
 * @date 2022-09-01
 */
@Service
public class SysRoleMenuServiceImpl implements SysRoleMenuService {

    @Autowired
    private SysRoleMenuMapper sysRoleMenuMapper;

    /**
     * 查询角色菜单
     * 
     * @param id ID
     * @return 单个对象
     */
    @Override
    @Transactional(readOnly = true)
    public SysRoleMenu selectById(String id) {

        return sysRoleMenuMapper.selectById(id);
    }

    /**
     * 查询角色菜单列表
     * 
     * @param param
     * @return 
     */
    @Override
    @Transactional(readOnly = true)
    public List<SysRoleMenu> selectList(SysRoleMenuParam param) {
    
        QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
        queryWrapper.eq(StringUtils.isNotBlank(param.getRoleId()), "role_id", param.getRoleId());
        queryWrapper.eq(StringUtils.isNotBlank(param.getMenuId()), "menu_id", param.getMenuId());
        queryWrapper.eq("type", param.getType());
        
        if(NumberUtil.eq(SysRoleMenuParam.TYPE_FRONT, param.getType())) {
        	queryWrapper.inSql("menu_id", "select id from sys_front_menu");
        }else {
        	queryWrapper.inSql("menu_id", "select id from sys_menu");
        }

        return sysRoleMenuMapper.selectList(queryWrapper);
    }

    /**
     * 新增角色菜单
     * 
     * @param sysRoleMenu 
     * @return 
     */
    @Override
    @Transactional
    public int insert(SysRoleMenuParam param) {
    	
		deleteByRoleId(param.getRoleId(), param.getType());
    	
    	if(param.getMenuId().equals("_NO_MENUS")) {
    		return 0;
    	}
    	
    	SysRoleMenu sysRoleMenu;
    	String[] ss = param.getMenuId().split(",");
    	List<SysRoleMenu> list = new ArrayList<>();
    	for (String menuId : ss) {
    		sysRoleMenu = new SysRoleMenu();
    		sysRoleMenu.setRoleId(param.getRoleId());
        	sysRoleMenu.setType(param.getType());
    		sysRoleMenu.setMenuId(menuId);
    		list.add(sysRoleMenu);
		}
    	
    	String sqlStatement = SqlHelper.getSqlStatement(SysRoleMenuMapper.class, SqlMethod.INSERT_ONE);
    	boolean flag = SqlHelper.executeBatch(SysRoleMenu.class, LogFactory.getLog(getClass()), list, 500, (sqlSession, entity) -> sqlSession.insert(sqlStatement, entity));
        return flag ? list.size() : 0;
    }
    
    @Override
    @Transactional
    public int deleteByRoleId(String roleId, int type) {

    	QueryWrapper<SysRoleMenu> queryWrapper = new QueryWrapper<>();
    	queryWrapper.eq("role_id", roleId);
    	queryWrapper.eq("type", type);
        return sysRoleMenuMapper.delete(queryWrapper);
    }
    
}
